package com.lb.videoplay.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lb.videoplay.R;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class CommonUtils {


    private Activity mActivity;
    private QMUITipDialog tipDialog = null;
    private Toast mToast;

    public CommonUtils(Activity activity) {
        mActivity = activity;
    }


    //加载Dialog
    public void showLoadDialog(String msg) {
        tipDialog = new QMUITipDialog.Builder(mActivity)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create();
        tipDialog.show();

    }

    //取消Dialog
    public void dissDialog() {
        if (tipDialog != null && tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }


    //展示token
    public void showToastCenter(String msg) {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        // 获取屏幕高度
        int height = display.getHeight();
        // 获取LayoutInflater对象
        LayoutInflater inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 由layout文件创建一个View对象
        View layout = inflater.inflate(R.layout.view_tosat, null);

        // 实例化TextView对象
        LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.view_toast);
        TextView textView = (TextView) layout.findViewById(R.id.toast_msg);
        textView.setText(msg);
        mToast = Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT);
        mToast.setView(layout);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }


    /*
     * bitmap转base64
     * */
    public String bitmapToBase64(Bitmap bitmap) {
        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * base64转为bitmap
     * @param base64Data
     * @return
     */
    public Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }
    /**
     * file转为base64
     * @param file
     * @return
     */
    public  String encodeBase64File(File file) throws Exception {
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer,Base64.DEFAULT);
    }

    //1、手机号开头集合
    //166，
    //176，177，178
    //180，181，182，183，184，185，186，187，188，189
    //145，147
    //130，131，132，133，134，135，136，137，138，139
    //150，151，152，153，155，156，157，158，159
    //198，199
    // 2、正则表达式
    public  boolean isChinaPhoneLegal(String str)
            throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
